<?php

namespace app\controllers\api;

use app\models\MobiNews;
use app\models\MobiNotice;
use Yii;

class MobileNoticeController extends ApiBaseController
{
    public function actionGetNewsList()
    {
        try {
            if (yii::$app->request->isPost) {
                $page_size = 5;
                $page = intval(yii::$app->request->post('page')) ?? 1;
                $data = MobiNotice::find()
                    ->orderBy('created_at desc')
                    ->offset(($page - 1) * $page_size)->limit($page_size)->all();
                $result = [];
                if (!empty($data)) {
                    foreach ($data as $value) {
                        $result[] = [
                            'url' => '/dist/images/news_wap/default.jpg',//封面图片
                            'title' => $value->title ?? '',
                            'auth' => $value->auth ?? '',
                            'text' => mb_substr(strip_tags($value->content), 0, 20).'...',
                            'link' => '/info/notice-detail?id='.$value->id,
                            'date' => date('m.d', strtotime($value->updated_at))
                        ];
                    }
                }
                return $this->ajaxMessage($result, 'success', 0);
            }
        } catch (\Exception $e) {
            return $this->ajaxMessage([], '服务器内部错误', 1);
        }
        return $this->ajaxMessage([], '错误的请求方式', 1);
    }
}
